package servlet;

import com.alibaba.fastjson.JSONArray;
import com.jiazhong.commons.bean.Emp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/sixth")
public class SixthServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        List<Emp> list = new ArrayList<>();
        Emp emp1 = new Emp(1, "张三", "程序员", 20, 8899, "2001-1-1", 5678.0, 123.0, 1);
        Emp emp2 = new Emp(2, "李四", "财务", 10, 7876, "2021-1-3", 3223.4, 123.0, 1);
        Emp emp3 = new Emp(3, "王五", "歌手", 20, 8899, "2012-1-6", 23232.1, 123.0, 1);

        list.add(emp1);
        list.add(emp2);
        list.add(emp3);
        // 将list--->json字符串
        String jsonString = JSONArray.toJSONString(list);
        // 将数据返回回去
        out.println(jsonString);
    }
}
